import ApiV1WxContactPageList_post from "@/api/ApiV1WxContactPageList_post";
import type { IRawContact } from "@/chatlog_types/IRawContact";
import { ContactType } from "@/constants/ContactType";
import { pinyin } from "pinyin-pro";
import type { IContactItem } from "./IContactItem";

export function contact_adapter(raw: IRawContact): IContactItem {
  const text = raw.nickName ?? raw.userName
  const py1 = pinyin(text, { pattern: 'pinyin' }).replace(/\s/g, '')
  const py2 = pinyin(text, { pattern: 'first' }).replace(/\s/g, '')
  const ret: IContactItem = {
    ...raw,
    nickName: raw.nickName || raw.userName,
    py1: py1,
    py2: py2,
    type: ContactType.guess_contact_type(raw.userName),
    raw: { ...raw }
  }
  return ret;
}

export function contact_adapter2(raw: ApiV1WxContactPageList_post.RespData): IContactItem {
  return contact_adapter({
    ...raw,
    userName: raw.userName ?? "",
    alias: raw.alias ?? "",
    remark: raw.remark ?? "",
    nickName: raw.nickName ?? "",
    isFriend: raw.isFriend ?? false,
    account: raw.account,
    src: raw,
  });
}
